package com.cetc.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

/**
 * Created by shea on 2018/10/19.
 */
//@Controller
public class DownloadDataController {
    public static void main(String[] args) throws Exception{

        Document doc = DocumentHelper.createDocument();
         //增加根节点
         Element HD = doc.addElement("ROWSET");
         //增加子元素
         Element ROW = HD.addElement("ROW");

         Element PTID = ROW.addElement("PTID");
         PTID.addAttribute("name","病例号");
         PTID.setText("123456789");

         Element NAME = ROW.addElement("NAME");
         //为子节点添加属性
         NAME.addAttribute("name","病人姓名");
         //为元素添加内容
         NAME.setText("张三");

         Element B_DATE = ROW.addElement("B_DATE");
         //为子节点添加属性
         B_DATE.addAttribute("name","出生日期");
         //为元素添加内容
         B_DATE.setText("2006-01-01");
         //添加注释
         ROW.addComment("YYYY-MM-DD的格式");

         Element GENDER = ROW.addElement("GENDER");
         //为子节点添加属性
         GENDER.addAttribute("name","性别");
         //为元素添加内容
         GENDER.setText("1");
         //添加注释行元素
         ROW.addComment("1.男 2.女");

         //实例化输出格式对象
         OutputFormat format = OutputFormat.createPrettyPrint();
         //设置输出编码
         format.setEncoding("UTF-8");
         //创建需要写入的File对象
         File file = new File("C:\\Users\\Shea\\Desktop" + File.separator + "test.xml");
         //生成XMLWriter对象，构造函数中的参数为需要输出的文件流和格式
         XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
         //开始写入，write方法中包含上面创建的Document对象
         writer.write(doc);
         writer.close();
    }
}
